package com.example.androidcomplete02.parser;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.androidcomplete02.R;

public class MyAdapter extends BaseAdapter {
	private ArrayList<Item> itemList;
	private LayoutInflater inflater;

	public MyAdapter(Context context, ArrayList<Item> itemList) {
		this.itemList = itemList;
		inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return itemList.size();
	}

	public String getItem(int position) {
		return itemList.get(position).getItemName();
	}

	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		TextView itemName;
		TextView makerName;
		TextView itemPrice;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View itemLayout = convertView;
		ViewHolder viewHolder = null;
		if (itemLayout == null) {
			itemLayout = inflater.inflate(R.layout.ch28_itemrow, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.itemName = (TextView) itemLayout
					.findViewById(R.id.itemName);
			viewHolder.makerName = (TextView) itemLayout
					.findViewById(R.id.makerName);
			viewHolder.itemPrice = (TextView) itemLayout
					.findViewById(R.id.itemPrice);
			itemLayout.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) itemLayout.getTag();
		}
		viewHolder.itemName.setText(itemList.get(position).getItemName());
		viewHolder.makerName.setText(itemList.get(position).getMakerName());
		viewHolder.itemPrice
				.setText(itemList.get(position).getItemPrice() + "");
		return itemLayout;
	}
}